package com.itheima.app.controller;

import cn.hutool.core.util.NumberUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    MessageManager messageManager;

    @Autowired
    UserManager userManager;

    @Autowired
    StringRedisTemplate redisTemplate;

    //环信用户认证
    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanxinUser(){
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.创建一个map,封装id和password
        Map<String,String> map = new HashMap<>();
        map.put("username",userId.toString());
        map.put("password","123456");

        //3.响应回去
        return ResponseEntity.ok(map);
    }

    @PostMapping("/tanhua/strangerQuestions")
    public ResponseEntity replyStrangerQuestion(@RequestBody Map<String,String>map){
        //获取推荐用户的id和回复的内容
        Long recommendUserId = NumberUtil.parseLong( map.get("userId"));
        String reply = map.get("reply");

        //调用manager完成操作
        return messageManager.replyStrangerQuestion(recommendUserId,reply);
    }

    //添加联系人
    @PostMapping("/messages/contacts")
    public ResponseEntity addContact(@RequestBody Map<String,Long> map){
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //转换数据库
        switchDatabase(1);
        //查询redis
        String userJson = redisTemplate.opsForValue().get(ConstantUtil.USER_ID + userId);
        if ("2".equals(userJson)){
            //转换数据库
            switchDatabase(0);
            return ResponseEntity.status(500).body("此操作已冻结");
        }else if (userJson==null){
            userManager.findUserupdate(userId);
        }
        //转换数据库
        switchDatabase(0);
        return messageManager.addContact(map.get("userId"));
    }

    //查询好友列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findContactsByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){
        return messageManager.findContactsByPage(pageNum,pageSize);
    }

    //查询点赞
    @GetMapping("/messages/likes")
    public ResponseEntity findLikesByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){
        return messageManager.findCommentsByPage(pageNum,pageSize,1);
    }

    //查询评论
    @GetMapping("/messages/comments")
    public ResponseEntity findCommentsByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){
        return messageManager.findCommentsByPage(pageNum,pageSize,2);
    }

    //查询喜欢
    @GetMapping("/messages/loves")
    public ResponseEntity findLovesByPage(
            @RequestParam(value = "page",defaultValue = "1")int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8")int pageSize){
        return messageManager.findCommentsByPage(pageNum,pageSize,3);
    }

    //TODO 公告列表
    @GetMapping("/messages/announcements")
    public ResponseEntity findAnnouncementByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "8") Integer pageSize
    ){
        return messageManager.findAnnouncementByPage(pageNum, pageSize,4);
    }
    public synchronized void  switchDatabase(int Dblndex){
        LettuceConnectionFactory connectionFactory=(LettuceConnectionFactory)redisTemplate.getConnectionFactory();
        //切换到指定的数据库
        connectionFactory.setDatabase(Dblndex);
        redisTemplate.setConnectionFactory(connectionFactory);
        //刷新配置
        connectionFactory.afterPropertiesSet();
        //重新连接
        connectionFactory.resetConnection();
    }
}
